# 首先 import 必要的模块
import pandas as pd
import numpy as np
from sklearn.metrics import log_loss
from xgboost import XGBClassifier
from pandas import DataFrame
import get_rid_of_2

allPdata=pd.read_csv("tao1_model/data/allPdata.csv",index_col=0)
allNdata=pd.read_csv("tao1_model/data/allNdata.csv",index_col=0)
allTdata=pd.read_csv("tao1_model/data/allTdata.csv",index_col=0)

def handclf(flag, howbig, zhengfu, allTdata, handoutlineset):
    if zhengfu == 1:
        one = set(allTdata[allTdata[flag] > howbig].index)
        handoutlineset = handoutlineset | one
        #         print(flag)
        #         print(len(handoutlineset))
        allTdata = allTdata.drop(allTdata[allTdata[flag] > howbig].index, axis=0)
        #         print(allTdata.shape)
        return handoutlineset, allTdata
    if zhengfu == 0:
        one = set(allTdata[allTdata[flag] < howbig].index)
        handoutlineset = handoutlineset | one
        #         print(flag)
        #         print(len(handoutlineset))
        allTdata = allTdata.drop(allTdata[allTdata[flag] < howbig].index, axis=0)
        #         print(allTdata.shape)
        return handoutlineset, allTdata

handoutline1=set()
handoutline2=set()
handoutline1,allPdata=handclf('c0',0.2,1,allPdata,handoutline1)
handoutline2,allNdata=handclf('c0',0.2,1,allNdata,handoutline2)
handoutline2,allNdata=handclf('c2',2,1,allNdata,handoutline2)
handoutline2,allNdata=handclf('c10',1.1,0,allNdata,handoutline2)
handoutline2,allNdata=handclf('c15',-0.4,0,allNdata,handoutline2)
handoutline2,allNdata=handclf('c16',2,1,allNdata,handoutline2)
handoutline2,allNdata=handclf('c17',1.1,1,allNdata,handoutline2)
handoutline2,allNdata=handclf('c20',1,1,allNdata,handoutline2)
handoutline1,allPdata=handclf('c20',1,1,allPdata,handoutline1)
handoutline2,allNdata=handclf('F_ai2_fft_frequency_max',2000,1,allNdata,handoutline2)
handoutline1,allPdata=handclf('FA1dataMean',4,1,allPdata,handoutline1)
handoutline2,allNdata=handclf('FA1dataVar',10,1,allNdata,handoutline2)
handoutline2,allNdata=handclf('ai2_frequency_max_diff',-1000,0,allNdata,handoutline2)
# print(allPdata.shape)
# print(allNdata.shape)
allPdata['lable']=1
allNdata['lable']=0
newallPdata=pd.concat([allPdata,allPdata,allPdata,allPdata],axis=0)
traindata=pd.concat([newallPdata,allNdata],axis=0)
y=traindata['lable']
x=traindata.drop('lable',axis=1)

allPdata['lable']=1
allNdata['lable']=0

newallPdata=pd.concat([allPdata,allPdata,allPdata,allPdata],axis=0)

traindata=pd.concat([newallPdata,allNdata],axis=0)
y=traindata['lable']
x=traindata.drop('lable',axis=1)

testXGBClassifier =  XGBClassifier(
        learning_rate =0.01,
        n_estimators=1157,  #数值大没关系，cv会自动返回合适的n_estimators
        max_depth=5,
        min_child_weight=1,
        gamma=0,
        subsample=0.8,
        colsample_bytree=0.6,
        colsample_bylevel=0.8,
        objective= 'binary:logistic',
        reg_alpha=0.01,
        reg_lambda=0.02,
        seed=3)

testXGBClassifier.fit(x,y)
xgboost_y=testXGBClassifier.predict_proba(allTdata)
xgboost_yDF=pd.DataFrame(xgboost_y,index=allTdata.index,columns=["p0",'p1'])
xgboost_yDF['result']=0
thread=xgboost_yDF['p1'].quantile(0.45)

xgboost_yDF.loc[xgboost_yDF['p1']>thread,'result']=1
xgboost_yDF=xgboost_yDF.drop(['p0','p1'],axis=1)

xgboost_yDF.to_csv('tao1_sub.csv',index_label='idx')
print("tao1_model scuessed!")

